package com.hc.tenant.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.hc.common.core.exception.tenant.TenantException;
import com.hc.common.core.utils.StringUtils;
import com.hc.common.datasource.DynamicSaasDataSource;
import com.hc.tenant.config.MqttConfig;
import com.hc.tenant.config.MqttPushClient;
import com.hc.tenant.domain.Dish;
import com.hc.tenant.domain.vo.MenusDishReqVo;
import com.hc.tenant.service.IMenusDishService;
import org.checkerframework.checker.units.qual.A;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.hc.common.log.annotation.Log;
import com.hc.common.log.enums.BusinessType;
import com.hc.common.security.annotation.PreAuthorize;
import com.hc.tenant.domain.Menus;
import com.hc.tenant.service.IMenusService;
import com.hc.common.core.web.controller.BaseController;
import com.hc.common.core.web.domain.AjaxResult;
import com.hc.common.core.utils.poi.ExcelUtil;
import com.hc.common.core.web.page.TableDataInfo;

/**
 * 菜谱Controller
 * 
 * @author yongjianz
 * @date 2021-02-06
 */
@RestController
@RequestMapping("/menus")
public class MenusController extends BaseController
{
    @Autowired
    private IMenusService menusService;

    @Autowired
    private IMenusDishService menusDishService;

    /**
     * 查询菜谱列表
     */
    @PreAuthorize(hasPermi = "tenant:menus:list")
    @GetMapping("/list")
    public TableDataInfo list(Menus menus)
    {
        startPage();
        List<Menus> list = menusService.selectMenusList(menus);
        return getDataTable(list);
    }

    /**
     * 导出菜谱列表
     */
    @PreAuthorize(hasPermi = "tenant:menus:export")
    @Log(title = "菜谱", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Menus menus) throws IOException
    {
        List<Menus> list = menusService.selectMenusList(menus);
        ExcelUtil<Menus> util = new ExcelUtil<Menus>(Menus.class);
        util.exportExcel(response, list, "menus");
    }

    /**
     * 获取菜谱详细信息
     */
    @PreAuthorize(hasPermi = "tenant:menus:query")
    @GetMapping(value = "/{menusId}")
    public AjaxResult getInfo(@PathVariable("menusId") Long menusId)
    {
        return AjaxResult.success(menusService.selectMenusById(menusId));
    }

    /**
     * 新增菜谱
     */
    @PreAuthorize(hasPermi = "tenant:menus:add")
    @Log(title = "菜谱", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Menus menus)
    {
        return toAjax(menusService.insertMenus(menus));
    }

    /**
     * 修改菜谱
     */
    @PreAuthorize(hasPermi = "tenant:menus:edit")
    @Log(title = "菜谱", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Menus menus)
    {
        return toAjax(menusService.updateMenus(menus));
    }

    /**
     * 删除菜谱
     */
    @PreAuthorize(hasPermi = "tenant:menus:remove")
    @Log(title = "菜谱", businessType = BusinessType.DELETE)
	@DeleteMapping("/{menusIds}")
    public AjaxResult remove(@PathVariable Long[] menusIds)
    {
        return toAjax(menusService.deleteMenusByIds(menusIds));
    }


    @GetMapping("/dishIds/{menusId}")
    public AjaxResult getDishesIdsByMenusId(@PathVariable Integer menusId) {
        return AjaxResult.success(menusDishService.selectDishIdsByMenusId(menusId));
    }

    @GetMapping("/dish/{menusId}")
    public AjaxResult getDishesByMenusId(@PathVariable Integer menusId) {
        List<Dish> dishList = menusDishService.selectDishesByMenusId(menusId);
        return AjaxResult.success(dishList);
    }

    @Log(title = "菜谱", businessType = BusinessType.INSERT)
    @PostMapping("/dish")
    public AjaxResult addDish(@RequestBody MenusDishReqVo menusDishReqVo) throws IOException {

        return AjaxResult.success(menusDishService.changeMenusDish(menusDishReqVo.getMenusId(), menusDishReqVo.getDishIds()));
    }

//    @Autowired
//    private DynamicSaasDataSource dc;
//    @Autowired
//    private MqttPushClient mq;
//    @Log(title = "菜谱", businessType = BusinessType.INSERT)
//    @GetMapping("/cc")
//    public AjaxResult gg() throws IOException {
//        System.out.println("datasoure: "+dc);
//        System.out.println("MqttPushClient: "+mq);
//        return AjaxResult.success();
//
//    }
}
